#include "common_lib/libtime.h"

using namespace common_lib;

LibTime LibTime::now()
{
    LibTime test;
    test.time = std::chrono::system_clock::now();
    test.duration  = std::chrono::time_point_cast<std::chrono::nanoseconds>(test.time).time_since_epoch();
    return test;
}
void LibTime::sleep(int second)
{
    std::this_thread::sleep_for(std::chrono::seconds(second));
}
LibTime LibTime::operator-(const LibTime &input) //类内重载，运算符重载函数作为类的成员函数
{
    LibTime ret;
    ret.duration = std::chrono::duration_cast<std::chrono::nanoseconds>(time - input.time);
    return ret;
}

LibTime LibTime::operator+(const int64_t & input)
{
    LibTime ret;
    ret.time = time + std::chrono::seconds(input);
    ret.duration  = std::chrono::time_point_cast<std::chrono::nanoseconds>(ret.time).time_since_epoch();
    return ret;
}

double LibTime::toSec()
{
    return (duration.count()*pow(10, -9));
}

double LibTime::toNSec()
{
    return duration.count();
}

int64_t LibTime::getSec()
{
    return (int64_t)(duration.count()*pow(10, -9));
}

int64_t LibTime::getNSec()
{
    return (duration.count() - getSec()*pow(10, 9));
}